﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class Manager_ProdList : System.Web.UI.PageEx
{
    private string strPageCode = "P0000C";
    private string strLoginResult = string.Empty;

    private int iPageCount = 0;
    private int iRecordCount = 0;
    private string strQuery = string.Empty;
    private string strProdCode = string.Empty;
    private string strSelectLang = "CN";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!DalEx.DbManagerEx.CheckLoginRight(UserType, UserId, strPageCode, ref strLoginResult))
        {
            FineUI.Alert.Show(strLoginResult, FineUI.MessageBoxIcon.Warning);
            Response.Redirect("Login.aspx");
        }
        if (!Page.IsPostBack)
        {
            ViewState["SelectLang"] = strSelectLang;
            DataBinds();
        }
    }
    private void DataBinds()
    {
        strSelectLang = Convert.ToString(ViewState["SelectLang"]);
        strQuery = Convert.ToString(ViewState["QueryString"]);
        List<Model.DbClass> lstClass = new List<Model.DbClass>();
        DalEx.DbClassEx.GetClassList(0, " and cType=1", ref lstClass);
        ddlSearchClass.DataSource = lstClass;
        ddlSearchClass.DataValueField = "CId";
        ddlSearchClass.DataTextField = "CName";
        ddlSearchClass.DataSimulateTreeLevelField = "CLevel";
        ddlSearchClass.DataBind();
        ddlSearchClass.Items.Insert(0, new FineUI.ListItem("--所有类别--", string.Empty));

        string strDBName = strSelectLang.CompareTo("EN")==0 ? "vdbEnProducts" : "dbProducts";
        DataSet ds = WebBase.Comm.SelectByPage(strDBName, "*", strQuery, "pId", "pId", 0, 1, gridList.PageSize, gridList.PageIndex + 1, out iPageCount, out iRecordCount);
        gridList.RecordCount = iRecordCount;
        gridList.DataSource = ds;
        gridList.DataBind();

        //if (strSelectLang.CompareTo("EN") == 0)
        //{
        //    FineUI.GridColumn gc = gridList.FindColumn("tfProdLang");
        //    gc.Visible = false;
        //    gc = gridList.FindColumn("lbtnEngListh");
        //    gc.Visible = false;
        //}
    }
    protected void btnCNVer_Click(object sender, EventArgs e)
    {
        ViewState["SelectLang"] = "CN";
        DataBinds();
    }
    protected void btnENVer_Click(object sender, EventArgs e)
    {
        ViewState["SelectLang"] = "EN";
        DataBinds();

    }
    protected void gridList_RowCommand(object sender, FineUI.GridCommandEventArgs e)
    {
        int iSelectId = 0;
        Dictionary<int, Dictionary<int, string>> modifiedDict = gridList.GetModifiedDict();
        string strCommand = e.CommandName;
        int iResult = 0;
        string strResult = string.Empty;
        bool bShowAlter = false;
        if (!string.IsNullOrEmpty(strCommand))
        {
            int iSelectIndexId = e.RowIndex;
            string strSelectId = gridList.Rows[iSelectIndexId].DataKeys[0].ToString();
            Int32.TryParse(strSelectId, out iSelectId);

            switch (strCommand)
            {
                case "Delete":
                    iResult = DalEx.DbProductsEx.Delete(" and pId=" + iSelectId, ref strResult);
                    bShowAlter = true;
                    break;
                case "Edit":
                    bShowAlter = true;
                    break;
                case "InsertColorImg":
                    Model.DbProducts dbProducts = DalEx.DbProductsEx.Select(iSelectId);
                    if (dbProducts != null)
                    {
                        winProdImage.Title = "产品（" + dbProducts.PName + "）颜色图片管理";
                        hfProdClassId.Text = dbProducts.PClassId.ToString();
                        hfProdCode.Text = dbProducts.PCode;
                        ViewState["ProdCode"] = dbProducts.PCode;
                        LoadProdImage(dbProducts.PCode);
                        winProdImage.Hidden = false;
                    }
                    break;
            }
            if (bShowAlter)
            {
                if (iResult > 0)
                {
                    FineUI.Alert.Show(strResult, FineUI.MessageBoxIcon.Information);
                    DataBinds();
                }
                else
                {
                    FineUI.Alert.Show(strResult, FineUI.MessageBoxIcon.Warning);
                }
            }
        }
    }
    protected void btnProdDelete_Click(object sender, EventArgs e)
    {
        string strResult = string.Empty;
        List<int> lstSelectRowId = WebBase.Comm.GetGridSeleteRowId(gridList);
        foreach (int iSelectId in lstSelectRowId)
        {
            DalEx.DbProductsEx.Delete(" and pId=" + iSelectId, ref strResult);
        }
        FineUI.Alert.Show(strResult, FineUI.MessageBoxIcon.Information);
        DataBinds();
    }
    protected void gridDataList_RowDataBound(object sender, FineUI.GridRowEventArgs e)
    {
        DataRowView drv = (DataRowView)gridList.Rows[e.RowIndex].DataItem;
        Model.DbClass dbClass = DalEx.DbClassEx.Select(Convert.ToInt32(drv["pClassId"]));
        if (dbClass!=null)
        {
            Label lblProdClass = (Label)gridList.Rows[e.RowIndex].FindControl("lblProdClass");
            lblProdClass.Text = dbClass.CName;
        }
    }
    protected void btnProdAdd_Click(object sender, EventArgs e)
    {
        winForm.IFrameUrl = "ProdAddNew.aspx";
        winForm.IFrameName = "新增产品资料";
        winForm.Title = "新增产品资料";
        winForm.Hidden = false;
    }
    protected void ttboxKeywords_Trigger1Click(object sender, EventArgs e)
    {
        ddlSearchClass.SelectedIndex = 0;
        ttboxKeywords.Text = string.Empty;
    }
    protected void ttboxKeywords_Trigger2Click(object sender, EventArgs e)
    {
        strQuery = string.Empty;
        string strSelectClass = ddlSearchClass.SelectedValue.Trim();
        string strKeywords = SqlHelper.String(ttboxKeywords.Text.Trim());
        if (!string.IsNullOrEmpty(strSelectClass))
            strQuery += " and pClassId = " + strSelectClass;
        if (!string.IsNullOrEmpty(strKeywords))
            strQuery += " and (pName like '%" + strKeywords + "%' or pNote like '%" + strKeywords + "%' or pCode='" + strKeywords + "')";
        ViewState["QueryString"] = strQuery;
        DataBinds();
        ddlSearchClass.SelectedValue = strSelectClass;
    }
    protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
    {
        int iPageSize = 20;
        int.TryParse(ddlPageSize.SelectedValue, out iPageSize);
        gridList.PageSize = iPageSize;
        DataBinds();
    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        DataBinds();
    }
    protected void winForm_Close(object sender, FineUI.WindowCloseEventArgs e)
    {
        DataBinds();
    }
    protected void gridImage_RowCommand(object sender, FineUI.GridCommandEventArgs e)
    {
        strProdCode = Convert.ToString(ViewState["ProdCode"]);
        int iSelectId = 0;
        Dictionary<int, Dictionary<int, string>> modifiedDict = gridImage.GetModifiedDict();
        string strCommand = e.CommandName;
        int iResult = 0;
        string strResult = string.Empty;
        if (!string.IsNullOrEmpty(strCommand))
        {
            int iSelectIndexId = e.RowIndex;
            string strSelectId = gridImage.Rows[iSelectIndexId].DataKeys[0].ToString();
            Int32.TryParse(strSelectId, out iSelectId);

            switch (strCommand)
            {
                case "Delete":
                    iResult = DalEx.DbProductsEx.ExDelete(" and PeId=" + iSelectId);
                    break;
                case "Modify":
                    if (modifiedDict.ContainsKey(iSelectIndexId))
                    {                        
                        Dictionary<int, string> rowDict = modifiedDict[iSelectIndexId];
                        Model.DbProductsEx dbProductsEx = Dal.DbProductsExDal.Select(iSelectId);
                        if (rowDict.ContainsKey(0))
                        {
                            dbProductsEx.PeName = SqlHelper.String(rowDict[0]);
                        } if (rowDict.ContainsKey(1))
                        {
                            dbProductsEx.PeEnName = SqlHelper.String(rowDict[1]);
                        }
                        iResult = Dal.DbProductsExDal.Update(dbProductsEx);
                    }
                    else
                    {
                        FineUI.Alert.Show("您没有修改任何信息不能保存！", FineUI.MessageBoxIcon.Warning);
                    }
                    break;
            }
            if (iResult > 0)
            {
                FineUI.Alert.Show("数据操作成功！", FineUI.MessageBoxIcon.Information);
                //DataBinds();
                LoadProdImage(strProdCode);
            }
            else
            {
                FineUI.Alert.Show("数据操作失败！", FineUI.MessageBoxIcon.Warning);
            }
        }
    }
    protected void btnSaveImage_Click(object sender, EventArgs e)
    {
        int iImageOrder = 99;
        int iProdClassId = 0;
        string strProdCode1 = SqlHelper.String(Convert.ToString(ViewState["ProdCode"]));
        string strImageName = SqlHelper.String(txtImageName.Text.Trim());
        string strEnImageName = SqlHelper.String(txtEnImageName.Text.Trim());
        string strImageOrder = txtImageOrder.Text.Trim();
        int.TryParse(strImageOrder, out iImageOrder);
        int.TryParse(Convert.ToString(hfProdClassId.Text), out iProdClassId);

        string strImageResult = string.Empty;
        string strImagePath = WebBase.Comm.GetFilePath(fuProdImage1, Server.MapPath("~/upload/products/" + iProdClassId + "/"), ResolveUrl("~/upload/products/" + iProdClassId + "/"), true, 0.6, AllowUpFileExt, ref strImageResult);//GetFilePath(fuProdImage1, iProdClassId, AllowUpFileExt, ref strImageResult);
        Model.DbProductsEx dbProductsEx = new Model.DbProductsEx();
        dbProductsEx.PeProdCode = strProdCode1;
        dbProductsEx.PeType = 9;//颜色图片
        dbProductsEx.PeValue = strImagePath;
        //string filename = System.IO.Path.GetFileName(strImagePath);//
        dbProductsEx.PeValue1 = strImagePath.Replace(ResolveUrl("~/upload/products/" + iProdClassId + "/"), ResolveUrl("~/upload/products/" + iProdClassId + "/") + "small_");//ResolveUrl("~/upload/products/" + iProdClassId + "/") + "small_" + filename;
        dbProductsEx.PeOrder = iImageOrder;
        dbProductsEx.PeName = strImageName;
        dbProductsEx.PeEnName = strEnImageName;

        if (Dal.DbProductsExDal.Insert(dbProductsEx) > 0)
        {
            LoadProdImage(strProdCode1);
            FineUI.Alert.Show("产品颜色图片上传成功！", FineUI.MessageBoxIcon.Information);
            sfImage.Reset();
        }
        else
        {
            FineUI.Alert.Show("产品颜色图片上传失败！", FineUI.MessageBoxIcon.Error);
        }
    }
    protected void winProdImage_Close(object sender, EventArgs e)
    {
        winProdImage.Hidden = true;
    }
    private void LoadProdImage(string strProdCode)
    {
        DataSet ds = Dal.DbProductsExDal.Select(" and peProdCode='" + strProdCode + "' and peType=9");
        gridImage.DataSource = ds;
        gridImage.DataBind();
    }
    protected void gridList_PageIndexChange(object sender, FineUI.GridPageEventArgs e)
    {
        gridList.PageIndex = e.NewPageIndex;
        DataBinds();
    }
}
